/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * actualizarUsua.java
 *
 * Created on 13/07/2011, 01:59:53 PM
 */
package mongolo;

import Biblioteca.Usua;
import Servicios.IMapeoObjetos;
import Servicios.MapeoObjetos;
import Servicios.SM;
import java.util.ArrayList;
import javax.swing.JOptionPane;

/**
 *
 * @author Pablo.Montero
 */
public class actualizarUsua extends javax.swing.JFrame {

    /** Creates new form actualizarUsua */
    public actualizarUsua() {
        initComponents();
        this.setDefaultCloseOperation(javax.swing.JFrame.HIDE_ON_CLOSE);
    }
    private boolean notYet = true;
    IMapeoObjetos mapo = new MapeoObjetos();

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        actualizarUsua_Ape2 = new javax.swing.JTextField();
        actualizarUsua_BTN = new javax.swing.JButton();
        actualizarUsua_Ape1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        actualizarUsua_Nom = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        actualizarUsua_ID = new javax.swing.JComboBox();
        eliminarUsua_BTN = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowActivated(java.awt.event.WindowEvent evt) {
                formWindowActivated(evt);
            }
        });

        jLabel1.setText("Codigo");

        actualizarUsua_BTN.setText("Actualizar Usuario");
        actualizarUsua_BTN.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                actualizarUsua_BTNActionPerformed(evt);
            }
        });

        jLabel2.setText("Nombre del usuario");

        jLabel4.setText("Apellido 2");

        jLabel3.setText("Apellido 1");

        actualizarUsua_ID.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                actualizarUsua_IDItemStateChanged(evt);
            }
        });

        eliminarUsua_BTN.setText("Actualizar Usuario");
        eliminarUsua_BTN.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                eliminarUsua_BTNActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(actualizarUsua_BTN)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(actualizarUsua_ID, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(actualizarUsua_Ape2, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(actualizarUsua_Ape1, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(actualizarUsua_Nom, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(eliminarUsua_BTN))
                .addContainerGap(48, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(actualizarUsua_ID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(actualizarUsua_Nom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(actualizarUsua_Ape1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(actualizarUsua_Ape2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(actualizarUsua_BTN)
                    .addComponent(eliminarUsua_BTN))
                .addContainerGap(32, Short.MAX_VALUE))
        );

        actualizarUsua_BTN.getAccessibleContext().setAccessibleName("Actualizar Usuario");
        eliminarUsua_BTN.getAccessibleContext().setAccessibleName("Eliminar Usuario");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void actualizarUsua_BTNActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_actualizarUsua_BTNActionPerformed
        String id = SM.ValidarNulls(actualizarUsua_ID.getSelectedItem(), "Error");
        String nom = SM.ValidarNulls(actualizarUsua_Nom.getText(), "Error");
        String ape1 = SM.ValidarNulls(actualizarUsua_Ape1.getText(), "Error");
        String ape2 = SM.ValidarNulls(actualizarUsua_Ape2.getText(), "Error");
        if (mapo.UpdateUsua(id, nom, ape1, ape2)) 
            JOptionPane.showMessageDialog(null, "Se actualizo el usuario");        
        else
           JOptionPane.showMessageDialog(null, SM.lastError);   
}//GEN-LAST:event_actualizarUsua_BTNActionPerformed

    private void actualizarUsua_IDItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_actualizarUsua_IDItemStateChanged
        String aa = SM.ValidarNulls(actualizarUsua_ID.getSelectedItem(), "ERROR");
        if (!" ".equals(aa)) {
            ArrayList<Usua> LisUsua = mapo.getUsua(aa);
            for (Usua u : LisUsua) {
                this.actualizarUsua_Nom.setText(u.nom);
                this.actualizarUsua_Ape1.setText(u.ape);
                this.actualizarUsua_Ape2.setText(u.ape2);
            }
        }
    }//GEN-LAST:event_actualizarUsua_IDItemStateChanged

    private void eliminarUsua_BTNActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_eliminarUsua_BTNActionPerformed
        String aa = SM.ValidarNulls(actualizarUsua_ID.getSelectedItem(), "Error");
        if (SM.cMes("Esta seguro que desea eliminar el usuario")) {
            if (!" ".equals(aa)) {
               SM.calcularDependencias(aa);               
               if(mapo.DeleteUsua(aa))
                   JOptionPane.showMessageDialog(null, "se elimno correctamente");
               else
                   JOptionPane.showMessageDialog(null, SM.lastError);   
            }
        }
    }//GEN-LAST:event_eliminarUsua_BTNActionPerformed

    private void formWindowActivated(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowActivated
        if (notYet) {
            notYet = false;
            ArrayList<Usua> LisUsua = mapo.getUsua();
            actualizarUsua_ID.addItem("");
            for (Usua u : LisUsua) {
                actualizarUsua_ID.addItem(u.id);
            }
        }
    }//GEN-LAST:event_formWindowActivated
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField actualizarUsua_Ape1;
    private javax.swing.JTextField actualizarUsua_Ape2;
    private javax.swing.JButton actualizarUsua_BTN;
    private javax.swing.JComboBox actualizarUsua_ID;
    private javax.swing.JTextField actualizarUsua_Nom;
    private javax.swing.JButton eliminarUsua_BTN;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    // End of variables declaration//GEN-END:variables
}
